package com.xtasz.itom.util;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;

/**
 * @author Marka
 * @date 2015/12/16
 * @Time 11:09
 */

public class StrutsUtil {
    private static final Logger logger = Logger.getLogger(StrutsUtil.class);

    /**
     * 返回 Application
     *
     * @return Application
     */
    public static ServletContext getApplication() {
        return ServletActionContext.getServletContext();
    }

    /**
     * 返回 HttpServletRequest
     *
     * @return HttpServletRequest
     */
    public static HttpServletRequest getRequest() {
        return ServletActionContext.getRequest();
    }

    /**
     * 返回 HttpServletResponse
     *
     * @return HttpServletResponse
     */
    public static HttpServletResponse getResponse() {
        return ServletActionContext.getResponse();
    }

    /**
     * 返回 Session
     *
     * @return HttpSession
     */
    public static HttpSession getSession() {
        return ServletActionContext.getRequest().getSession();
    }

    /**
     * 返回 Cookies
     *
     * @return Cookie[]
     */
    public static Cookie[] getCookie() {
        return getRequest().getCookies();
    }

    /**
     * 根据 name 返回 Cookie 中相应的值
     *
     * @param name
     * @return
     */
    public static String getCookieValueByName(String name) {
        if (getRequest().getCookies() == null) {
            return "";
        }
        for (Cookie cookie : getRequest().getCookies()) {

            if (name.equals(cookie.getName())) {
                try {
                    return URLDecoder.decode(cookie.getValue(), "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    logger.warn(e.getMessage(), e);
                }
            }
        }
        return "";
    }

    /**
     * 根据 name 返回 Cookie 中相应的值
     *
     * @param name
     * @param request
     * @return
     */
    public static String getCookieValueByName(String name, HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (name.equals(cookie.getName())) {
                    return cookie.getValue();
                }
            }
        }
        return "";
    }

    /**
     * 设置Cookie相应的值
     *
     * @param name
     * @param value
     */
    public static void setCookieValue(String name, String value) {
        if (name != null && value != null) {
            try {
                Cookie cookie = new Cookie(name, URLEncoder.encode(value, "UTF-8"));
                // TODO to be implemented (get value from configuration file)
                cookie.setPath("/");
                cookie.setMaxAge(365 * 24 * 60 * 60);
                getResponse().addCookie(cookie);
            } catch (Exception e) {
                logger.warn(e.getMessage(), e);
            }
        }
    }

    /**
     * 移除cookie
     * @param name
     * @param value
     */
    public static void removeCookieValue(String name, String value) {
        if (name != null && value != null) {
            try {
                Cookie cookie = new Cookie(name, URLEncoder.encode(value, "UTF-8"));
                // TODO to be implemented (get value from configuration file)
                cookie.setPath("/");
                cookie.setMaxAge(0);
                getResponse().addCookie(cookie);
            } catch (Exception e) {
                logger.warn(e.getMessage(), e);
            }
        }
    }

    /**
     * 根据 name 返回 Application 中相应的值
     *
     * @param name
     * @return
     */
    public static String getContextParamByName(String name) {
        return getApplication().getInitParameter(name);
    }

    /**
     *
     * 返回 request 参数
     *
     * @param paramName
     * @return
     */
    public static String getReqParam(String paramName) {
        return ServletActionContext.getRequest().getParameter(paramName);
    }


    /************校验方法*********/
    public static boolean isEmpty(Map<String, String> map, String key) {
        if (map == null) {
            return false;
        }

        if (map.get(key) == null || "".equals(map.get(key))) {
            return false;
        }
        return true;
    }

    public static String getIp() {
        return getRequest().getRemoteAddr();
    }
}
